/**
 * 
 */
package org.gaming.db.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author YY
 *
 */
public class QueryUtil {

	public static class QueryCondition {
		private String filter;
		private List<Object> values = new ArrayList<>();
		public QueryCondition(String filter, Object... values) {
			this.filter = filter;
			for(Object value : values) {
				this.values.add(value);
			}
		}
	}
	
	public static class QuerySet {
		private List<QueryCondition> conditions = new ArrayList<>();
		private int page;	//从1开始
		private int pageSize;
		private String orderBy;
		private String groupBy;
		
		private String where;
		private Object[] params;
		
		private String countWhere;
		private Object[] countParams;
		
		public void addCondition(QueryCondition condition) {
			conditions.add(condition);
		}
		
		/**
		 * @param filter 数据库的字段名
		 * @param values
		 */
		public void addCondition(String filter, Object... values) {
			conditions.add(new QueryCondition(filter, values));
		}
		
		public void findInSet(String columnName, Collection<?> values) {
			conditions.add(new QueryCondition("FIND_IN_SET(" + columnName + ", ?)", QueryUtil.join(values, ",")));
		}
		
		public void orderBy(String orderBy) {
			this.orderBy = orderBy;
		}
		
		public void groupBy(String groupBy) {
			this.groupBy = groupBy;
		}
		
		public void limit(int page, int pageSize) {
			if(page <= 0) {
				page = 1;
			}
			if(pageSize <= 0) {
				pageSize = 50;
			}
			this.page = page;
			this.pageSize = pageSize;
		}
		
		public void formWhere() {
			String where = "";
			List<Object> params = new ArrayList<>();
			if(!conditions.isEmpty()) {
				where = "where ";
				boolean isFirst = true;
				for(QueryCondition condition : conditions) {
					if(!isFirst) {
						where += " and ";
					}
					where += condition.filter;
					params.addAll(condition.values);
					isFirst = false;
				}
			}
			this.countWhere = where;
			this.countParams = params.toArray();
			if(this.groupBy != null) {
				where += " " + this.groupBy;
			}
			if(this.orderBy != null) {
				where += " " + this.orderBy;
			}
			if(pageSize > 0) {
				where += " limit ?, ?";
				int startIndex = (page - 1) * pageSize;
				params.add(startIndex);
				params.add(pageSize);
			}
			this.where = where;
			this.params = params.toArray();
		}

		public String getWhere() {
			return where;
		}

		public Object[] getParams() {
			return params;
		}

		public String getCountWhere() {
			return countWhere;
		}

		public Object[] getCountParams() {
			return countParams;
		}
		
		public void print() {
			System.out.println(where);
			System.out.println(Arrays.toString(params));
			System.out.println(countWhere);
			System.out.println(Arrays.toString(countParams));
		}
	}
	
	public static String join(Collection<?> objs, String sperator) {
		StringBuilder builder = new StringBuilder();
		boolean isFirst = true;
		for(Object obj : objs) {
			if(!isFirst) {
				builder.append(sperator);
			}
			builder.append(obj.toString());
			isFirst = false;
		}
		return builder.toString();
	}
	
	
}
